package com.paw.arg.resolver.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.paw.arg.resolver.resolver.TrafficResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lvzihai
 * @date 2021/6/25
 **/
@Service
public class TrafficService {

  @Autowired(required = false)
  List<TrafficResolver> trafficResolverList = new ArrayList<>();

  public Object traffic(String type,Object argument){
    System.out.printf("trafficResolverList "+trafficResolverList.size());
    TrafficResolver resolver = getFirstResolver(type);
    if(resolver!=null){
      return resolver.resolver(argument);
    }

    return "traffic not find. type: "+type;
  }

  private TrafficResolver getFirstResolver(String type){
    TrafficResolver resolver = trafficResolverList.stream()
        .filter(item -> item.support(type))
        .findFirst()
        .orElse(null);
    return resolver;
  }

}
